#include<bits/stdc++.h>
#define K 25
#define N 220000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
    char ch=0;
    int x=0,flag=1;
    while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*flag;
}
int a[N],lg[N],f[N][K];
int query(int l,int r){int k=lg[r-l+1];return max(f[l][k],f[r-(1<<k)+1][k]);}
int main()
{
	int n=read(),m=read();
	for(int i=1;i<=n;i++)a[i]=read(),f[i][0]=a[i];
	for(int i=1;i<=n;i++)lg[i]=lg[i-1]+(1<<(lg[i-1]+1)==i);
	for(int k=1;(1<<k)<=n;k++)for(int i=1;i+(1<<k)-1<=n;i++)f[i][k]=max(f[i][k-1],f[i+(1<<(k-1))][k-1]);
	for(int i=1;i<=m;i++){int l=read(),r=read();printf("%d\n",query(l,r));}
	return 0;
}
